package cj.web.tb.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.domain.UserInfo;
import cj.web.login.controllers.BaseController;
import cj.web.tb.constants.GameTypeEnum;
import cj.web.tb.constants.IsCheckEnum;
import cj.web.tb.constants.PayStatusEnum;
import cj.web.tb.dao.AreaDAO;
import cj.web.tb.dao.ComposeOnlineOrderDAO;
import cj.web.tb.dao.ComposeSubjectDAO;
import cj.web.tb.dao.GameDAO;
import cj.web.tb.dao.GrageDAO;
import cj.web.tb.dao.SchoolDAO;
import cj.web.tb.domain.ComposeOnlineOrder;
import cj.web.tb.domain.ComposeSubject;

/**
 * 网络作文老师评卷
 * @author Administrator
 *
 */
@Path("/onlineCheck")
public class COnlineCheckController extends BaseController{

	private static Logger logger = Logger.getLogger(COnlineCheckController.class.getName());
	
	@Autowired
	private ComposeOnlineOrderDAO composeOnlineOrderDao;
	@Autowired
	private GrageDAO gradeDao;
	@Autowired
	private AreaDAO areaDao;
	@Autowired
	private SchoolDAO schoolDao;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private ComposeSubjectDAO composeSubjectDAO;
	
	@Get("show")
	public String show(Model model,ComposeOnlineOrder composeOnlineOrder) {
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		model.add("IsCheckStatus", EnumSet.allOf(IsCheckEnum.class));
		return "/views/online/onlineOrderCheckList.vm";
	}
	
	@Post("getList")
	public String queryList(ComposeOnlineOrder composeOnlineOrder,HttpServletRequest request) {
		
		//当前页  
		composeOnlineOrder.setStart((composeOnlineOrder.getPage()-1)*composeOnlineOrder.getRows());
		composeOnlineOrder.setLength(composeOnlineOrder.getRows());
		JSONObject jo = new JSONObject();
		
		UserInfo uc = returnSessionObj(request);
		composeOnlineOrder.setTeacherId(uc.getId());
		
		List<ComposeOnlineOrder> objs = composeOnlineOrderDao.queryCheck(composeOnlineOrder);
		
		for(ComposeOnlineOrder obj:objs){
			obj.setStatusName(PayStatusEnum.getName(obj.getStatus()));
			obj.setIsCheckName(IsCheckEnum.getName(obj.getIsCheck()));
		}
		
		int size = composeOnlineOrderDao.queryChecksize(composeOnlineOrder);
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		ComposeOnlineOrder composeOnlineOrder = composeOnlineOrderDao.queryById(id);
		model.add("obj", composeOnlineOrder);
		Map game = gameDao.queryById(composeOnlineOrder.getGameId()).get(0); 
		model.add("game", game);
        
        ComposeSubject composeSubject = composeSubjectDAO.queryById(composeOnlineOrder.getSubjectId());
        model.add("composeSubject", composeSubject);
		return "/views/online/onlineOrderCheckEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,ComposeOnlineOrder composeOnlineOrder,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		try {
			composeOnlineOrder.setIsCheck(IsCheckEnum.YPG.getValue());
			composeOnlineOrderDao.updateCheckStatus(composeOnlineOrder);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
}